﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class CopyrightService : ICopyrightService
    {
        private ICopyrightManager _icm;
        public CopyrightService(ICopyrightManager icm)
        {
            _icm = icm;
        }

        public async Task<int> AddUsersAsync(string title, string details, string images, string logo, string address, string tel1, string tel2, string qq1, string qq2, string email)
        {
            return await _icm.AddAsync(new Model.Copyright 
            {
                Title = title,
                Details = details,
                Images = images,
                Logo = logo,
                Address = address,
                Tel1 = tel1,
                Tel2 = tel2,
                QQ1 = qq1,
                QQ2 = qq2,
                Email = email
            });
        }

        public async Task<int> DeleteUsersAsync(int id)
        {
            var data = await _icm.GetById(id);
            if(data == null)
                return -1;
            if(data.DeleteId != 1)
                return -2;
            return await _icm.DeleteAsync(data);
        }

        public async Task<List<CopyrightDto>> GetAllUsersAsync(int deleteId = 1)
        {
            var data = await _icm.GetChooseAsync(cp => cp.DeleteId == deleteId);
            return data == null ? new List<CopyrightDto>() : data.Select(cp => new CopyrightDto 
            {
                Id = cp.Id,
                Title = cp.Title,
                Details = cp.Details,
                Images = cp.Images,
                Logo = cp.Logo,
                Address = cp.Address,
                Tel1 = cp.Tel1,
                Tel2 = cp.Tel2,
                QQ1 = cp.QQ1,
                QQ2 = cp.QQ2,
                Email = cp.Email,
                CreateTime = cp.CreateTime,
                UpdateTime = cp.UpdateTime
            }).ToList();
        }

        public async Task<List<CopyrightDto>> GetDateByName(string title, int deleteid = 1)
        {
            var data = await _icm.GetChooseAsync(cp => cp.Title.Contains(title) && cp.DeleteId == deleteid);
            return data == null ? new List<CopyrightDto>() : data.Select(cp => new CopyrightDto 
            {
                Id = cp.Id,
                Title = cp.Title,
                Details = cp.Details,
                Images = cp.Images,
                Logo = cp.Logo,
                Address = cp.Address,
                Tel1 = cp.Tel1,
                Tel2 = cp.Tel2,
                QQ1 = cp.QQ1,
                QQ2 = cp.QQ2,
                Email = cp.Email,
                CreateTime = cp.CreateTime,
                UpdateTime = cp.UpdateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _icm.GetById(id);
            if(data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _icm.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _icm.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _icm.UpdateAsync(data);
        }

        public async Task<int> UpdateUsersAsync(int id, string title, string details, string images, string logo, string address, string tel1, string tel2, string qq1, string qq2, string email)
        {
            var data = await _icm.GetById(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.Details = details;
            data.Images = images;
            data.Logo = logo;
            data.Address = address;
            data.Tel1 = tel1;
            data.Tel2 = tel2;
            data.QQ1 = qq1;
            data.QQ2 = qq2;
            data.Email = email;
            data.UpdateTime = DateTime.Now;
            return await _icm.UpdateAsync(data);
        }
    }
}
